package com.jinglin.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinglin.component.EsSearchComponent;
import com.jinglin.component.RedisComponent;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.constants.Constants;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.enums.*;
import com.jinglin.entity.po.UserAction;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.po.VideoInfoFile;
import com.jinglin.entity.query.UserActionQuery;
import com.jinglin.entity.vo.PaginationResultVO;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.entity.vo.VideoInfoResultVo;
import com.jinglin.exception.BusinessException;
import com.jinglin.mapper.UserActionMapper;
import com.jinglin.redis.RedisData;
import com.jinglin.redis.RedisUtils;
import com.jinglin.service.VideoInfoFileService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.web.annotation.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频信息 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/video")
public class VideoController extends ABaseController {
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private VideoInfoFileService  videoInfoFileService;

    @Autowired
    private RedisComponent redisComponent;

    @Autowired
    private RedisUtils redisUtils;

    @PostMapping("/loadRecommendVideo")
    public ResponseVO loadRecommendVideo(){
        LambdaQueryWrapper<VideoInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoInfo::getRecommendType, VideoRecommendTypeEnum.RECOMMEND.getType());
        List<VideoInfo> list = videoInfoService.list(wrapper);
        return getSuccessResponseVO(list);
    }
    @PostMapping("/loadVideo")
    public ResponseVO loadVideo(String pCategoryId,String categoryId,Integer pageNo){
        Page<VideoInfo> objectPage = new Page<>(pageNo, PageSize.SIZE15.getSize());
        LambdaQueryWrapper<VideoInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoInfo::getRecommendType,VideoRecommendTypeEnum.NO_RECOMMEND.getType());
        wrapper.eq(pCategoryId!=null && !pCategoryId.equals(""),VideoInfo::getPCategoryId,pCategoryId);
        wrapper.eq(categoryId!=null && !categoryId.equals(""),VideoInfo::getCategoryId,categoryId);
        Page<VideoInfo> page = videoInfoService.page(objectPage, wrapper);
        PaginationResultVO<VideoInfo> resultVO = new PaginationResultVO<>();
        resultVO.setPageSize(15);
        resultVO.setPageNo(pageNo);
        resultVO.setPageTotal((int)page.getPages());
        resultVO.setTotalCount((int) page.getTotal());
        resultVO.setList(page.getRecords());
        return getSuccessResponseVO(resultVO);
    }
    @PostMapping("/getVideoInfo")
    public ResponseVO getVideoInfo(@NotEmpty String videoId){
        VideoInfoResultVo videoInfoResultVo = videoInfoService.getVideoInfo(videoId, getTokenUserInfoDto());
        return getSuccessResponseVO(videoInfoResultVo);
    }
    @PostMapping("/loadVideoPList")
    public ResponseVO loadVideoPList(@NotEmpty String videoId){
        LambdaQueryWrapper<VideoInfoFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoInfoFile::getVideoId,videoId);
        wrapper.orderByAsc(true,VideoInfoFile::getFileIndex);
        List<VideoInfoFile> list = videoInfoFileService.list(wrapper);
        return getSuccessResponseVO(list);
    }
    // 这个接口是为了保持用户的在线状态。用户进行观看视频时，没过五秒向这个接口发送一次心跳，证明用户正在观看。
    // 我们将信息保存到redis中，redis设置用户信息8(大于5)秒自动过期。我们发送心跳，维持key的生命周期，来达到用户在线观看的效果
    @PostMapping("/reportVideoPlayOnline")
    public ResponseVO reportVideoPlayOnline(@NotEmpty String fileId,@NotEmpty String deviceId){
        Integer count = videoInfoService.reportVideoPlayOnline(fileId, deviceId);
        return getSuccessResponseVO(count);
    }


    @RequestMapping("/search")
    public ResponseVO search(@NotEmpty String keyword, Integer orderType, @RequestParam(defaultValue = "0") Integer pageNo,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        redisComponent.addKeywordCount(keyword);
        // 剔除ES
//        PaginationResultVO resultVO = esSearchComponent.search(true, keyword, orderType, pageNo, PageSize.SIZE30.getSize());
        PaginationResultVO<VideoInfo> result = videoInfoService.searchVideos(
                keyword,
                orderType,
                pageNo,
                pageSize
        );
        return getSuccessResponseVO(result);
    }

    @RequestMapping("/getVideoRecommend")
    public ResponseVO getVideoRecommend(@NotEmpty String keyword, @NotEmpty String videoId) {
        // 剔除ES
/*
        List<VideoInfo> videoInfoList = esSearchComponent.search(false, keyword, SearchOrderTypeEnum.VIDEO_PLAY.getType(), 1, PageSize.SIZE10.getSize()).getList();
*/
/*
        videoInfoList = videoInfoList.stream().filter(item -> !item.getVideoId().equals(videoId)).collect(Collectors.toList());
*/
        return getSuccessResponseVO(null    );
    }
    @RequestMapping("/loadHotVideoList")
    public ResponseVO loadHotVideoList(Integer pageNo){
        PaginationResultVO<VideoInfo> videoInfoPaginationResultVO = videoInfoService.loadHotVideoList(pageNo);
        return getSuccessResponseVO(videoInfoPaginationResultVO);
    }

    @RequestMapping("/getSearchKeywordTop")
    @GlobalInterceptor
    public ResponseVO getSearchKeywordTop() {
        List<String> keywordList = redisComponent.getKeywordTop(Constants.LENGTH_10);
        return getSuccessResponseVO(keywordList);
    }
}

